//
//  FavHelper.m
//  FavClient
//
//  Created by MagicStudio on 12-4-17.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "FavHelper.h"
#import "FavClientAppDelegate.h"
#import "LoginController.h"

/*#define SubType_PageCurl = @"pageCurl"//向上翻一页
#define SubType_PageUnCurl = @"pageUnCurl"//向下翻一页
#define SubType_RippleEffect = @"rippleEfect"//水滴效果
#define SubType_SuckEffect = @"suckEffect"//收缩效果,如一块布被抽走
#define SubType_Cube = @"cube"//立方休效果
#define SubType_OglFilp = @"oglFilp"//上下翻页效果*/



@implementation UIAlertViewHelper

//创建一个新的UIAlertView作为ProgressDialog使用
// msg : 需要显示的文字信息
// delegate : UIAlertViewDelegate 实例，这里主要是用来改变ProgressDialog的大小
// size : 需要生成的ProgressDialog的大小
+(UIAlertView *) showProgressDialog:(NSString *)msg delegate:(id)delegate alertSize:(CGSize)size{
    UIAlertView *loadAlertView = [[[UIAlertView alloc] initWithTitle:nil message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil] autorelease];
    UIActivityIndicatorView *loadingBar = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge] autorelease];
    CGSize msgSize = [msg sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:size lineBreakMode:UILineBreakModeWordWrap] ;
    UITextView *msgTextView = [[UITextView alloc] initWithFrame:CGRectMake((size.width - msgSize.width - 10) / 2, (size.height - 50 - msgSize.height) / 2, msgSize.width + 20, msgSize.height + 20)];
    msgTextView.text = msg ;
    UIFont *font = [UIFont systemFontOfSize:15] ;
    [msgTextView setTextColor:[UIColor whiteColor]] ;
    [msgTextView setBackgroundColor:[UIColor clearColor]] ;
    [msgTextView setFont:font] ;
    [msgTextView setTextAlignment:UITextAlignmentCenter] ;
    [loadAlertView setDelegate:delegate] ;
    [loadingBar setCenter:CGPointMake((size.width - 10) / 2,  size.height - 50.0)] ;
    [loadingBar startAnimating] ;
    [loadAlertView addSubview:loadingBar] ;
    [loadAlertView addSubview:msgTextView] ;
    [loadAlertView show] ;
    return loadAlertView ;
}




@end


@implementation ViewChangeHelper


#pragma Mark-

//从自己切换到另一个视力控制器
//使用UIViewAnimationTransitionFlipFromRightf动画
+(void) changeControllerForSelf : (UIViewController *) appearController disAppearController : (UIViewController * ) disAppearController{
    [UIView beginAnimations:@"View Animation" context:nil] ;
    [UIView setAnimationDuration:1] ;
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut] ;
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:disAppearController.view cache:YES] ;
    [appearController viewWillAppear:YES];
    [disAppearController viewWillDisappear:YES];
    for(UIView *subView in disAppearController.view.subviews){
        [subView removeFromSuperview] ;
    }
    
     [appearController viewDidAppear:YES];
     [disAppearController viewDidDisappear:YES];
    [disAppearController.view insertSubview:appearController.view atIndex:0] ;
    [UIView commitAnimations];
}


//从自己切换到另一个视力控制器
// anim : 使用什么样的动画方式进行切换
+(void) changeControllerForSelf : (UIViewController *) appearController disAppearController : (UIViewController * ) disAppearController animation:(UIViewAnimationTransition)anim{
    [UIView beginAnimations:@"View Animation" context:nil] ;
    [UIView setAnimationDuration:1] ;
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut] ;
    [UIView setAnimationTransition:anim forView:disAppearController.view cache:YES] ;
    [appearController viewWillAppear:YES];
    [disAppearController viewWillDisappear:YES];
    for(UIView *subView in disAppearController.view.subviews){
        [subView removeFromSuperview] ;
    }
    
    [appearController viewDidAppear:YES];
    [disAppearController viewDidDisappear:YES];
    [disAppearController.view insertSubview:appearController.view atIndex:0] ;
    [UIView commitAnimations];
}

//使用CATransition动画进行UIView切换
+(void) changeControllerForSelf : (UIViewController *) appearController disAppearController : (UIViewController * ) disAppearController catTrasition : (CATransition *) anim{
    [disAppearController.view.layer addAnimation:anim forKey:@"anim"] ;
    [disAppearController.view addSubview:appearController.view] ;
}

#pragma Mark-

@end

@implementation UIViewAnimationHelper
          
//开始一个动画
+(void) startAnimation : (NSString *) animName{
    [UIView beginAnimations:animName context:nil] ;
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut] ;
    [UIView setAnimationDuration:1] ;
    //self.logo.alpha = 1 ;
    //[self.logo setFrame:CGRectMake(42, 47, 233, 82 )] ;
    //self.mVersion.alpha = 1 ;
    //[self.mVersion setFrame:CGRectMake(101, 326, 115, 21)] ;
    //[UIView commitAnimations] ;
}

//结束动画设置,并执行这个动画
+(void) endAnimation{
    [UIView commitAnimations] ;
}

//根据索引获得对应的CATransition属性subType对象的值 
+(NSString *) getSubTypeByIndex : (NSInteger) index{
    switch(index){
        case Type_PageCurl: return @"pageCurl" ;break ;
        case Type_PageUnCurl: return @"pageUnCurl" ;break ;
        case Type_RippleEffect : return @"rippleEffect" ; break ;
        case Type_SuckEffect: return @"suckEffect" ;break ;
        case Type_Cube: return @"cube" ;break ;
        case Type_OglFilp: return @"oglFilp" ;break ;
        case Type_Swirl: return @"swirl" ;break ;
        case Type_ZoomyOut :return @"zoomyOut" ;break ;
        case Type_SpewEffect : return @"spewEffect" ; break ;
        case Type_CameraIrisHollowOpen :return @"cameraIrisHollowOpen" ;break ;
        case Type_CameraIrisHollowClose :return @"cameraIrisHollowClose" ;break ;
        default:return @"pageCurl" ;
    }
}

//setTpe
//kCATransitionFade淡出
//kCATransitionMoveIn覆盖原图
//kCATransitionPush推出
//kCATransitionReveal底部显出来

//setSubtype:也可以有四种类型：
//kCATransitionFromRight；
//kCATransitionFromLeft(默认值)
//kCATransitionFromTop；
//kCATransitionFromBottom

//执行一个自定义动画
//type 设置类型
//subType : 设置方向
+(void) doCATAnimationDefault : (UIView *) view duration:(CGFloat)duration type:(NSString *) type subType : (NSString *)subType{
    CATransition *animation = [CATransition animation] ;
    [animation setDuration:duration] ;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]] ;
    [animation setType:type] ;
    [animation setSubtype:subType] ;
    [view.layer addAnimation:animation forKey:@"doCatAnimation"] ;
}

+(void) doCATAnimation:(UIView *)view duration:(CGFloat)duration  type:(NSInteger)type {
    CATransition *animation = [CATransition animation] ;
    [animation setDuration:duration] ;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]] ;
    //animation.type = [UIViewAnimationHelper getSubTypeByIndex:subType] ;
    animation.type = [UIViewAnimationHelper getSubTypeByIndex:type];
    animation.subtype = kCATransitionFromLeft ;
    [view.layer addAnimation:animation forKey:@"doCatAnimation1"] ;
}

+(void) doCATAnimation:(UIView *)view duration:(CGFloat)duration  type:(NSInteger)type subType:(NSString *)subType  {
    CATransition *animation = [CATransition animation] ;
    [animation setDuration:duration] ;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]] ;
    //animation.type = [UIViewAnimationHelper getSubTypeByIndex:subType] ;
    animation.type = [UIViewAnimationHelper getSubTypeByIndex:type];
    animation.subtype = subType ;
    [view.layer addAnimation:animation forKey:@"doCatAnimation1"] ;
}

//黑夜使用kCATransitionFromLeft
+(CATransition *) getCATAnimation :(CGFloat)duration  type:(NSInteger)type {
    CATransition *animation = [CATransition animation] ;
    [animation setDuration:duration] ;
    [animation setTimingFunction:UIViewAnimationOptionCurveEaseInOut] ;
    animation.type = [UIViewAnimationHelper getSubTypeByIndex:type];
    animation.subtype = kCATransitionFromLeft ;
    return animation ;
}

//自定义subType
+(CATransition *) getCATAnimation :(CGFloat)duration  type:(NSInteger)type subType:(NSString *)subType{
    CATransition *animation = [CATransition animation] ;
    [animation setDuration:duration] ;
    [animation setTimingFunction:UIViewAnimationOptionCurveEaseInOut] ;
    animation.type = [UIViewAnimationHelper getSubTypeByIndex:type];
    animation.subtype = subType;
    return animation ;
}

@end

